/**
 * MainFrame.java
 * Version: 1.0
 * Created in 06/08/2013
 * Created by Gerson Groth
 * 
 * Copyright
 */
package webcrawler.activities.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.border.EmptyBorder;

import org.apache.log4j.Logger;

import webcrawler.activities.view.download.DesktopDownload;
import webcrawler.activities.view.download.SequencialDownload;
import webcrawler.activities.view.download.WebDownload;
import webcrawler.activities.view.login.LoginDialog;

/**
 * The Class MainFrame.
 * 
 * @author Gerson Groth
 */
@SuppressWarnings("serial")
public class MainFrame extends JFrame {

	/** The logger. */
	private Logger logger = Logger.getLogger(MainFrame.class);

	/** The content pane. */
	private JPanel contentPane;

	// private JDesktopPane desktopPane;

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("Garmin Web Crawler");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		// create DesktopPane
		// desktopPane = new JDesktopPane();
		// desktopPane.setBounds(0, 267, 434, -265);
		// contentPane.add(desktopPane);

		createMenu();

	}

	/**
	 * Creates the menu.
	 */
	private void createMenu() {
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 434, 21);
		contentPane.add(menuBar);

		// Menu File
		JMenu jmFile = createMenuFile();
		menuBar.add(jmFile);

		JMenu mnLogin = new JMenu("Login");
		menuBar.add(mnLogin);

		JMenuItem mntmLogin = new JMenuItem("Login");
		mntmLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				LoginDialog login = new LoginDialog();
				login.setVisible(true);
			}
		});
		mnLogin.add(mntmLogin);

		JMenu mnDownload = createMenuDownload();
		menuBar.add(mnDownload);

		// TODO - Menu Help
		JMenu jmHelp = new JMenu("Help");
		menuBar.add(jmHelp);

		// TODO - Item Menu About
		JMenuItem jmiAbout = new JMenuItem("About");
		jmHelp.add(jmiAbout);

	}

	/**
	 * Creates the menu download.
	 * 
	 * @return the j menu
	 */
	private JMenu createMenuDownload() {
		JMenu mnDownload = new JMenu("Download");

		JMenuItem mntmWebSearch = new JMenuItem("Web Activities Search");
		mntmWebSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				webDownload();
			}
		});
		mnDownload.add(mntmWebSearch);

		JMenuItem mntmDesktopActivitiesSearch = new JMenuItem(
				"Desktop Activities Search");
		mntmDesktopActivitiesSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				desktopDownload();
			}
		});
		mnDownload.add(mntmDesktopActivitiesSearch);

		JSeparator separator = new JSeparator();
		mnDownload.add(separator);

		JMenuItem mntmSequencialActivities = new JMenuItem(
				"Sequencial Activities");
		mntmSequencialActivities.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sequencialDownload();
			}

		});
		mnDownload.add(mntmSequencialActivities);

		return mnDownload;
	}

	/**
	 * 
	 */
	protected void desktopDownload() {
		DesktopDownload desktop = new DesktopDownload();
		desktop.setVisible(true);
		
	}

	/**
	 * Web download.
	 */
	protected void webDownload() {
		WebDownload web = new WebDownload();
		web.setVisible(true);

	}

	/**
	 * Creates the menu file.
	 * 
	 * @return JMenu
	 */
	private JMenu createMenuFile() {
		JMenu jmFile = new JMenu("File");

		// Menu Item Options
		/*
		 * Comentado, pois estava sendo usado somente para alterar entre
		 * browsers e, para usar o ie e o chrome havia a necessidade de
		 * executáveis para windows e setar os caminhos para o linux
		 */
		// JMenuItem jmOptions = createMenuItemOptions();
		// jmFile.add(jmOptions);

		// JSeparator separator = new JSeparator();
		// jmFile.add(separator);

		JMenuItem jmiExit = createMenuItemExit();
		jmFile.add(jmiExit);

		return jmFile;
	}

	/**
	 * Creates the menu item exit.
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem createMenuItemExit() {
		JMenuItem jmiExit = new JMenuItem("Exit");
		jmiExit.setMnemonic('E');
		jmiExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_F4,
				java.awt.event.InputEvent.ALT_MASK));
		jmiExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exit();
			}

		});
		return jmiExit;
	}

	/**
	 * Creates the menu item options.
	 * 
	 * @return JMenuItem
	 */
	private JMenuItem createMenuItemOptions() {
		JMenuItem jmOptions = new JMenuItem("Options");
		jmOptions.setMnemonic('O');
		jmOptions.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_O,
				java.awt.event.InputEvent.CTRL_MASK));
		jmOptions.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				OptionsDialog options = new OptionsDialog();
				options.setVisible(true);
			}
		});
		return jmOptions;
	}

	/**
	 * Sequencial download.
	 */
	private void sequencialDownload() {
		SequencialDownload sequencial = new SequencialDownload();
		sequencial.setVisible(true);
	}

	/**
	 * Exit the application.
	 */
	private void exit() {
		this.dispose();
		System.exit(0);
	}
}
